/**
 * Created by Dylan on 2017/4/24.
 */
var Trends;
if (!Trends) {
    Trends = {}
}
var trendSelectPath = "http://192.168.0.161:9096/web"
var trendPath = "http://47.105.128.56:9097/api/interact"
Trends.operateFormatter = function (value, row, index) {
    return [
        '<a class="detail" href="javascript:void(0)" title="编辑"><i class="glyphicon glyphicon-search"></i></a>' +
        '&nbsp;&nbsp;&nbsp;&nbsp;' +
        '<a class="comment" href="javascript:void(0)" title="评论"><i class="glyphicon glyphicon-list-alt"></i></a>' +
        '&nbsp;&nbsp;&nbsp;&nbsp;' +
        '<a class="remove" href="javascript:void(0)" title="删除"><i class="fa fa-trash-o"></i></a>'
    ].join('');
}
Trends.hostIcon = function (value, row, index) {
    var url = File_Download_Url + "/" + value;
    if (value == null || value == "") {
        var url = File_Download_Url + "/" + value;
        return [
            '<img src="' + $().AbsUrlParse("/static/images/defaultUser/img_50_50_none@3x.png") + '" width="50px" height="50px;" onclick="enlargeImage(this)"/>'
        ].join('');
    } else {
        var url = File_Download_Url + "/" + value;
        return [
            '<img src="' + url + '" width="50px" height="50px;" onclick="enlargeImage(this)"/>'
        ].join('');
    }
}

Trends.commentOperateFormatter = function (value, row, index) {
    return [
        '<a class="replay" href="javascript:void(0)" title="回复"><i class="glyphicon glyphicon-list-alt"></i></a>' +
        '&nbsp;&nbsp;&nbsp;&nbsp;' +
        '<a class="removeComment" href="javascript:void(0)" title="删除"><i class="fa fa-trash-o"></i></a>'
    ].join('');
}

Trends.replayOperateFormatter = function (value, row, index) {
    return [
        '<a class="removeReplay" href="javascript:void(0)" title="删除"><i class="fa fa-trash-o"></i></a>'
    ].join('');
}

Trends.replayTypeOperateFormatter = function (value, row, index) {
    if (value == 1) {
        return [
            '<p>楼主</p>'
        ].join('');
    } else {
        return [
            '<p>非楼主</p>'
        ].join('');
    }
}

window.operateEvents = {
    'click .detail': function (e, value, row, index) {
        showDetail(row);
    },
    'click .comment': function (e, value, row, index) {
        showComments(row);
    },
    'click .replay': function (e, value, row, index) {
        showReplay(row);
    },
    'click .remove': function (e, value, row, index) {
        comment.showOperationTips("确定删除吗？", function () {
            comment.get(trendPath + '/web/deleteDynamic?trendId=' + row.trendId, null, function (data) {
                if (data.res.code == 40000) {
                    // hint("删除成功");
                    comment.refresh();
                } else {
                    hint(data.res.msg);
                }
            });
        });
    },
    'click .removeComment': function (e, value, row, index) {
        comment.showOperationTips("确定删除吗？", function () {
            comment.get(trendPath + '/web/deleteComments?commentId='+row.commentId, null, function (data) {
                if (data.res.code == 40000) {
                    // hint("删除成功");
                    var table = $("#commentTable");
                    var url = $().AbsUrlParse(trendSelectPath + "/listComments?trendId=" + row.trendId);
                    table.bootstrapTable("refresh", {url: url});
                } else {
                    hint(data.res.msg);
                }
            });
        });
    },
    'click .removeReplay': function (e, value, row, index) {
        comment.showOperationTips("确定删除吗？", function () {
            comment.get(trendPath + '/web/deleteReply?replyId=' + row.replyId + "&commentId=" + row.commentId, null, function (data) {
                if (data.res.code == 40000) {
                    // hint("删除成功");
                    var table = $("#replayTable");
                    var url = $().AbsUrlParse(trendSelectPath+"/findReply?commentId=" + row.commentId);
                    table.bootstrapTable("refresh", {url: url});
                } else {
                    hint(data.res.msg);
                }
            });
        });
    },
    'click .showImgs': function (e, value, row, index) {
        showImages(row);
    },
    'click .showVideoImg': function (e, value, row, index) {
        $("#images").empty();
        var videoPicPath = row.videoPic;
        var url = File_Download_Url;
        showModel("detailVideoPic-div", function () {
            var picPath = url + "/" + videoPicPath;
            $("#videoPic").attr("src", picPath)
        });
    },
    'click .showVideo': function (e, value, row, index) {
        showVideo(row);
    },
    'click .showVoice': function (e, value, row, index) {
        showVoice(row);
    }
}

var showImages = function (row) {
    $("#images").empty();
    var picPath = row.trendsImages;
    var picStr = '';
    var picPathArray = picPath.split(',');
    var url = File_Download_Url;
    showModel("detailImages-div", function () {
        $.each(picPathArray, function (n, value) {
            var everyUrl = url + "/" + value;
            picStr += '<image src="' + everyUrl + '" style="height: 300px;" ><br/>';
        })
        $("#images").append(picStr)
    });
}

var showVoice = function (row) {
    showModel("detailVoice-div", function () {
        var url = File_Download_Url;
        var allUrl = url + "/" + row.trendsVideo
        $("#voice").attr("src", allUrl);
    });
}

var showDetail = function (row) {
    showModel("detailTrends-div", function () {
        $("#detailContent").html(row.comment);
        $("#detail").val(row.comment);
    });
}

var showComments = function (row) {
    showModel("trendsComments-div", function () {
        var table = $("#commentTable");
        var url = $().AbsUrlParse(trendSelectPath + "/listComments?trendId=" + row.trendId);
        table.bootstrapTable("refresh", {url: url});
    });
}

var showReplay = function (row) {
    showModel("trendsReplay-div", function () {
        var table = $("#replayTable");
        var url = $().AbsUrlParse(trendSelectPath + "/findReply?commentId=" + row.commentId);
        table.bootstrapTable("refresh", {url: url});
    });
}

var showVideo = function (row) {
    showModel("detailVideo-div", function () {
        var url = File_Download_Url + "/" + row.trendsVideo;
        $("#video").attr("src", url);
    });
}

operateImages = function (value, row, index) {
    if (value.length == null || value == '') {
        return ["-"].join('');
    } else {
        return ['<a class="showImgs" href="javascript:void(0)" title="查看图片"><i >点击查看</i></a>'].join('');
    }
}

operateAvPic = function (value, row, index) {
    if (value == null || value == '') {
        return ["-"].join('');
    } else {
        return ['<a class="showVideoImg" href="javascript:void(0)" title="查看图片"><i >点击查看</i></a>'].join('');
    }
}

operateVideos = function (value, row, index) {
    if (value == null || value == '') {
        return ["-"].join('');
    } else {
        return ['<a class="showVideo" href="javascript:void(0)" title="查看视频"><i >点击查看</i></a>'].join('');
    }
}

operateVoice = function (value, row, index) {
    if (value == null || value == '') {
        return ["-"].join('');
    } else {
        return ['<a class="showVoice" href="javascript:void(0)" title="查看音频"><i >点击查看</i></a>'].join('');
    }
}

//查询商品
bindClick("#trendSelect", function () {
    var userName = $("input[name=userName]").val();

    var url = $().AbsUrlParse(trendSelectPath+"/findTrends?name=" + userName);
    $("#table").bootstrapTable("refresh", {url: url});
});